package com.imooc.imoocbilibili.controller;

import com.imooc.imoocbilibili.service.FileService;
import com.imooc.imoocbilibili.utils.FastDFSUtil;
import com.imooc.imoocbilibili.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileApi {

    @Autowired
    private FileService fileService;
    @Autowired
    private FastDFSUtil fastDFSUtil;

    @PostMapping("/md5files")
    public R getFileMD5(MultipartFile file) throws Exception {
        String fileMD5 = fileService.getFileMD5(file);
        return R.ok(fileMD5);
    }

    @PutMapping("/file-slices")
    public R uploadFileBySlices(MultipartFile slice,
                                String fileMd5,
                                Integer sliceNo,
                                Integer totalSliceNo) throws Exception {
        String s = fastDFSUtil.uploadCommonFile(slice);
//        String filePath = fileService.uploadFileBySlices(slice, fileMd5, sliceNo, totalSliceNo);
        return R.ok(s);
    }

    /**
     * 为了方便测试,增加一个后端对文件分片方法
     * 实际开发中是前端对文件分片后,调用uploadFileBySlices()方法
     */
    @GetMapping("/slices")
    public R slices(MultipartFile file) throws Exception {
        fileService.slices(file);
        return R.ok();
    }
}
